package repo

import (
	"github.com/jonasngs/go_entry_task/protobuf3/pb"
	"github.com/jonasngs/go_entry_task/tcp_server/repo/impl"
	"github.com/jonasngs/go_entry_task/tcp_server/storage"
	"log"
)

type IUserDao interface {
	FetchUser(username string) (*pb.User, error)
	UpdateNickName(username string, nickname string) error
	UpdateProfilePicture(username string, profilePicture string) error
}

func InitializeDaoService(db storage.Database) IUserDao {
	// createTable(db)
	log.Printf("DB services 初始化")
	return new(impl.UserDaoServiceImpl)
}

func createTable(db storage.Database) {
	query := `CREATE TABLE IF NOT EXISTS t_user` +
		`(user_id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL,
					username VARCHAR(32) NOT NULL,
					password VARCHAR(64) NOT NULL,
					nickname VARCHAR(32) NOT NULL DEFAULT '', 
					profile_picture VARCHAR(160) NOT NULL DEFAULT '',
					created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
					updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
					PRIMARY KEY(user_id)
					) ENGINE = INNODB COLLATE utf8mb4_unicode_ci`
	_, err := db.DB.Exec(query)
	if err != nil {
		log.Printf("Error %s when creating Table\n", err)
	}
}
